package com.shopmall.manager.controller;

import com.github.pagehelper.PageInfo;
import com.shopmall.manager.service.ProductService;
import com.shopmall.model.dto.product.ProductDto;
import com.shopmall.model.entity.product.Product;
import com.shopmall.model.vo.common.Result;
import com.shopmall.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    // 商品上下架
    @GetMapping("/updateStatus/{id}/{status}")
    public Result<String> updateStatus(@PathVariable Long id,
                                       @PathVariable Integer status) {
        String flag = productService.updateStatus(id, status);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 审核
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result<String> updateAuditStatus(@PathVariable Long id,
                                            @PathVariable Integer auditStatus) {
        String flag = productService.updateAuditStatus(id, auditStatus);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 删除
    @DeleteMapping("/deleteById/{id}")
    public Result<String> deleteById(@Parameter(name = "id", description = "商品id", required = true)
                                     @PathVariable Long id) {
        String flag = productService.deleteById(id);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 保存修改数据
    @PutMapping("/updateById")
    public Result<String> update(@RequestBody Product product) {
        String flag = productService.update(product);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 根据商品id查询商品信息
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        Product product = productService.getById(id);
        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    // 添加
    @PostMapping("/save")
    public Result<String> save(@RequestBody Product product) {
        String flag = productService.save(product);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 列表，条件分页查询
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Product>> list(@PathVariable("page") Integer page,
                                          @PathVariable("limit") Integer limit,
                                          ProductDto productDto
    ) {
        PageInfo<Product> pageInfo = productService.findByPage(page, limit, productDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
